package com.yl.common.ui

import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.FrameLayout
import cn.jzvd.JzvdStd
import com.bumptech.glide.Glide
import com.google.gson.Gson
import com.yl.common.base.BaseActivity
import com.yl.common.bean.DailyModel
import com.yl.common.util.AppGlobal
import com.yl.common.R
import com.yl.common.databinding.ActivityPlayVideoBinding

class PlayVideoActivity : BaseActivity<ActivityPlayVideoBinding>() {
    var videoModel: DailyModel.Issue.Item? = null
    override fun getLayoutID(): Int = R.layout.activity_play_video

    override fun init() {
        val bundle = intent.getBundleExtra("Message")
        val data = bundle!!.getString("data")
        videoModel = Gson().fromJson(data, DailyModel.Issue.Item::class.java)
        Glide.with(AppGlobal.get()!!).load(videoModel!!.data.cover!!.blurred)
            .into(mBind.mVideoBackground)
        addNormalVideoView()
        mBind.mSwipeRefreshLayout.setOnRefreshListener {
            getRelateVideoList()
        }

        mBind.title.text = videoModel!!.data.title
        mBind.category.text = videoModel!!.data.category
        mBind.description.text = videoModel!!.data.description
        mBind.collectionCount.text = videoModel!!.data.consumption.collectionCount.toString()
        mBind.shareCount.text = videoModel!!.data.consumption.shareCount.toString()
        mBind.replyCount.text = videoModel!!.data.consumption.replyCount.toString()
        mBind.author.text = videoModel!!.data.author!!.name
        Glide.with(AppGlobal.get()!!).load(videoModel!!.data.author)
            .into(mBind.img)

//        mBind.mRecyclerView
    }

    private fun getRelateVideoList() {
//        mViewModel.getRelateVideoList(videoModel.id).observerKt {
//            mAdapter.addData(it)
//        }
    }

    private fun addNormalVideoView() {
        mBind.mSurfaceContainer.viewTreeObserver
            .addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
                override fun onPreDraw(): Boolean {
                    mBind.mSurfaceContainer.viewTreeObserver.removeOnPreDrawListener(this)
                    val jzvdStd = JzvdStd(this@PlayVideoActivity).apply {
                        setUp(videoModel!!.data.playUrl, videoModel!!.data.title)
                        startVideo()
                    }
                    mBind.mSurfaceContainer.addView(
                        jzvdStd, FrameLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT
                        )
                    )
                    return true
                }
            })
    }

}